using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;
using Utilities.Security;

public partial class Admin_Users_UserAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
   
        // Page.Title = MultipleLanguage.OWNSITE + " - " + MultipleLanguage.USER_DETAIL.ToLower();
        if (!IsPostBack)
        {
            BindDataToDropdown();
            ValidateControl();
            BindDataWhenEdit();
        }
    }

    private void BindDataToDropdown()
    {
        UserRoles objRole = new UserRoles();
        DataTable dtbRole = new DataTable();
        dtbRole = objRole.Search();
        ddlRole.DataSource = dtbRole;
        ddlRole.DataValueField = "UserRoleID";
        ddlRole.DataTextField = "Description";
        ddlRole.DataBind();
    }

    private void ValidateControl()
    {
        int nCurrentLanguage = Support.GetDefaultLanguage();
        nCurrentLanguage = nCurrentLanguage - 1;
        lblConfirmPassword.Text = MultipleLanguage.LBL_CONFIRM_PASSWORD[nCurrentLanguage];
        lblEmail.Text = MultipleLanguage.LBL_EMAIL[nCurrentLanguage];
        lblPassword.Text = MultipleLanguage.LBL_PASSWORD[nCurrentLanguage];
        lblRole.Text = MultipleLanguage.LBL_ROLE[nCurrentLanguage];
        lblUsername.Text = MultipleLanguage.LBL_FULLNAME[nCurrentLanguage];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nCurrentLanguage];
        btnCancel.Text = MultipleLanguage.BTN_BACK[nCurrentLanguage]; 
        rfvUsername.ErrorMessage = MultipleLanguage.USERNAME_EMPTY[nCurrentLanguage];
        rfvEmail.ErrorMessage = MultipleLanguage.EMAIL_EMPTY[nCurrentLanguage];
        rfvPassword.ErrorMessage = MultipleLanguage.PASSWORD_EMPTY[nCurrentLanguage];
        revEmail.ErrorMessage = MultipleLanguage.EMAIL_WRONG[nCurrentLanguage];
        revPassword.ErrorMessage = MultipleLanguage.PASSWORD_SPACE_REMOVE[nCurrentLanguage];
        cpvPassword.ErrorMessage = MultipleLanguage.PASSWORD_NOT_SAME_CONFIRM_PASSWORD[nCurrentLanguage];
        lbleActive.Text = MultipleLanguage.LBL_ACTIVE[nCurrentLanguage];
        
    }

    private void BindDataWhenEdit()
    {
        string sAction = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        try
        {
            if (sAction == Config.ACTION_EDIT)
            {
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nID > 0)
                {
                    Users objUser = new Users();
                    objUser.LoadById(nID);
                    txtEmail.Text = ConvertData.ConvertToString(objUser.Data.Email);
                    txtUserName.Text = ConvertData.ConvertToString(objUser.Data.Username);
                    ddlRole.SelectedValue = ConvertData.ConvertToString(objUser.Data.RoleID);

                    int nActive = ConvertData.ConvertToInt(objUser.Data.Status);
                    if (nActive > 0)
                        chkActive.Checked = true;
                    else
                        chkActive.Checked = false;
                }
            }
        }
        catch
        {
            string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_USER;
            Response.Redirect(sLink);
        }
    }


    protected void btnSave_Click1(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        Users objUser = new Users();
        objUser.Data.Email = txtEmail.Text.Trim();
        objUser.Data.Username = txtUserName.Text.Trim();
        objUser.Data.RoleID = ConvertData.ConvertToInt(ddlRole.SelectedValue);
        if (chkActive.Checked)
            objUser.Data.Status = EnumeType.ACTIVE;
        else
            objUser.Data.Status = EnumeType.INACTIVE;

        ClsEncrypt objEnCrypt = new ClsEncrypt();
        objUser.Data.Password = objEnCrypt.Encrypt(txtPassword.Text);

        int nResult = 0;
        string sLink = "";
        string sAction = Request.QueryString[Config.ACTION];
        switch (sAction)
        {
            case Config.ACTION_ADD:
                nResult = objUser.Insert();
                if (nResult > 0)
                {
                    sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_USER + "&" + Config.MESS_ID + "=" + Config.MESSAGE_INSERT; 
                    Response.Redirect(sLink);
                }
                else
                    MessageBoxss.Show(MessagesAlert.INSERT_UNSUCCESSFUL[nDefaultLanguage]);
                
                break;
            case Config.ACTION_EDIT:
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                nResult = objUser.Update(nID);
                if (nResult > 0)
                {
                    MessageBoxss.Show(MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);

                }
                else
                    MessageBoxss.Show(MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage]);
                break;
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string sGoToPageMan = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_USER;
        Response.Redirect(sGoToPageMan);
    }
}
